#include "formsetget.h"
#include "ui_formsetget.h"

FormSetGet::FormSetGet(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSetGet)
{
    ui->setupUi(this);
    currentIndex = 0;

    //sooooooooooooooo hard-coded...
    ui->ComboBoxConfig->addItem("PU_CONTROL_UNDEFINED");
    ui->ComboBoxConfig->addItem("PU_BACKLIGHT_COMPENSATION_CONTROL");
    ui->ComboBoxConfig->addItem("PU_BRIGHTNESS_CONTROL");
    ui->ComboBoxConfig->addItem("PU_CONTRAST_CONTROL");
    ui->ComboBoxConfig->addItem("PU_GAIN_CONTROL");
    ui->ComboBoxConfig->addItem("PU_POWER_LINE_FREQUENCY_CONTROL");
    ui->ComboBoxConfig->addItem("PU_HUE_CONTROL");
    ui->ComboBoxConfig->addItem("PU_SATURATION_CONTROL");
    ui->ComboBoxConfig->addItem("PU_SHARPNESS_CONTROL");
    ui->ComboBoxConfig->addItem("PU_GAMMA_CONTROL");
    ui->ComboBoxConfig->addItem("PU_WHITE_BALANCE_TEMPERATURE_CONTROL");
    ui->ComboBoxConfig->addItem("PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL");
    ui->ComboBoxConfig->addItem("PU_WHITE_BALANCE_COMPONENT_CONTROL");
    ui->ComboBoxConfig->addItem("PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL");
    ui->ComboBoxConfig->addItem("PU_DIGITAL_MULTIPLIER_CONTROL");
    ui->ComboBoxConfig->addItem("PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL");
    ui->ComboBoxConfig->addItem("PU_HUE_AUTO_CONTROL");
    ui->ComboBoxConfig->addItem("PU_ANALOG_VIDEO_STANDARD_CONTROL");
    ui->ComboBoxConfig->addItem("PU_ANALOG_LOCK_STATUS_CONTROL");
}

FormSetGet::~FormSetGet()
{
    delete ui;
}

void FormSetGet::on_ComboBoxConfig_currentIndexChanged(int index)
{
    currentIndex = index;
    ui->LineEditMax->setText("");
    ui->LineEditMin->setText("");
    ui->LineEditRes->setText("");
    ui->LineEditGetValue->setText("");
    ui->LineEditSetValue->setText("");
}

void FormSetGet::setFile(int file)
{
    fileHandle = file;
    if (fileHandle <= 0)
        close();
}

void FormSetGet::on_PushButtonGet_clicked()
{
    int retval;
    long value;
    unsigned long arg;
    QString tmpStr;
    //check something ? file ? command in range ?
    //min
    arg = 0x00ff0000 & ((unsigned long)(GET_MIN)<< 16);
    arg |= (((unsigned char)currentIndex)<<8);

    retval = ioctl(fileHandle, IOCTL_GET, arg);
    tmpStr.setNum(retval);
    ui->LineEditMin->setText(tmpStr);
    //max
    arg = 0x00ff0000 & ((unsigned long)(GET_MAX)<< 16);
    arg |= (((unsigned char)currentIndex)<<8);
    retval = ioctl(fileHandle, IOCTL_GET, arg);
    tmpStr.setNum(retval);
    ui->LineEditMax->setText(tmpStr);
    //res
    arg = 0x00ff0000 & ((unsigned long)(GET_RES)<< 16);
    arg |= (((unsigned char)currentIndex)<<8);
    retval = ioctl(fileHandle, IOCTL_GET, arg);
    tmpStr.setNum(retval);
    ui->LineEditRes->setText(tmpStr);
    //Value
    arg = 0x00ff0000 & ((unsigned long)(GET_CUR)<< 16);
    arg |= (((unsigned char)currentIndex)<<8);
    retval = ioctl(fileHandle, IOCTL_GET, arg);
    tmpStr.setNum(retval);
    ui->LineEditGetValue->setText(tmpStr);
}

void FormSetGet::on_PushButtonSet_clicked()
{
    unsigned long arg;
    unsigned long valueToSend;
    //int retval;
    valueToSend = ui->LineEditSetValue->text().toLong();
    arg = 0xff000000 & ((unsigned long)currentIndex<<24);
    arg |= ((unsigned long)valueToSend);
    //retval =
    ioctl(fileHandle, IOCTL_SET, arg);
}
